<template>
  <div class="exhibitors-directory">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <el-carousel height="400px" arrow="never" indicator-position="none">
        <el-carousel-item v-for="item in bannerImage" :key="item.id">
          <img :src="item.imageUrl" :alt="item.title" class="banner-img" />
        </el-carousel-item>
      </el-carousel>
      <div class="container">
        <h1 class="page-title">展品商城</h1>
        <p class="page-subtitle">查看展品信息</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <!-- <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibitors-directory')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibitors-directory' },
            ]"
          >
            展品商城
          </a>
        </nav>
      </div>
    </div> -->

    <div class="container">
      <section class="directory-section">
        <!-- <h2 class="section-title">展商产品</h2> -->
        <div class="directory-content">
          <!-- <div class="directory-intro">
            <h3>产品展示</h3>
            <p>
              汇集来自全球展商的最新机器人产品和技术，为观众提供全面的产品信息展示。
            </p>
          </div> -->

          <!-- 搜索框 -->
          <div class="search-section">
            <div class="search-container">
              <el-input
                v-model="searchKeyword"
                placeholder="请输入产品名称或展商名称进行搜索..."
                size="large"
                clearable
                @input="handleSearch"
                class="search-input"
              >
                <template #prefix>
                  <el-icon><Search /></el-icon>
                </template>
              </el-input>
            </div>
            <div class="search-stats" v-if="searchKeyword">
              找到 {{ filteredProducts.length }} 个产品
            </div>
          </div>

          <!-- 产品网格 -->
          <div class="directory-grid">
            <div
              class="product-item"
              v-for="product in filteredProducts"
              :key="product.id"
              @click="showProductDetail(product)"
            >
              <div class="product-image">
                <img
                  :src="
                    product.imageUrl || product.image || defaultProductImage
                  "
                  :alt="product.productName"
                  v-if="product.imageUrl || product.image"
                />
                <div class="no-image" v-else>
                  <el-icon><Picture /></el-icon>
                </div>
              </div>
              <div class="product-info">
                <h4>{{ product.productName }}</h4>
                <p class="exhibitor-name">{{ product.exhibitorName }}</p>
                <p class="product-category" v-if="product.category">
                  {{ product.category }}
                </p>
                <!-- <div class="product-action">
                  <span class="view-detail">查看详情 →</span>
                </div> -->
              </div>
            </div>
          </div>

          <!-- 分页 -->
          <div class="pagination-section">
            <el-pagination
              v-model:current-page="currentPage"
              :page-size="pageSize"
              :total="total"
              layout="prev, pager, next, jumper"
              @current-change="handlePageChange"
            />
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, computed } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { Search, Picture } from "@element-plus/icons-vue";
import { productApi, subpageApi, homepageApi } from "@/api/user";

export default {
  name: "ExhibitorsDirectory",
  components: {
    Search,
    Picture,
  },
  setup() {
    const activeTab = ref("exhibitors-directory");
    const router = useRouter();

    // 产品数据
    const products = ref([]);
    const bannerImage = ref("");
    const defaultProductImage = ref(
      "https://via.placeholder.com/300x200/E9ECFF/320063?text=Product"
    );

    // 搜索相关
    const searchKeyword = ref("");

    // 分页相关
    const currentPage = ref(1);
    const pageSize = ref(12);
    const total = ref(0);
    // const totalPages = computed(() => Math.ceil(total.value / pageSize.value));

    // 加载产品数据
    const loadProducts = async () => {
      try {
        const params = {
          page: currentPage.value,
          size: pageSize.value,
          keyword: searchKeyword.value || undefined,
        };

        const response = await productApi.getPublicProductPage(params);
        if (response && (response.code === "200" || response.code === 200)) {
          if (response.data && response.data.records) {
            // 分页数据格式
            products.value = response.data.records.map((item, index) => ({
              id: item.id || index + 1,
              productName: item.productName || "",
              exhibitorName: item.exhibitorName || "",
              category: item.productCategory || "",
              description: item.productDescription || "",
              specifications: item.priceRange || "",
              imageUrl: item.productImage || "",
              image: item.productImage || "",
              exhibitorId: item.exhibitorId || "",
            }));
            total.value = response.data.total || 0;
          } else if (Array.isArray(response.data)) {
            // 数组格式
            products.value = response.data.map((item, index) => ({
              id: item.id || index + 1,
              productName: item.productName || "",
              exhibitorName: item.exhibitorName || "",
              category: item.productCategory || "",
              description: item.productDescription || "",
              specifications: item.priceRange || "",
              imageUrl: item.productImage || "",
              image: item.productImage || "",
              exhibitorId: item.exhibitorId || "",
            }));
            total.value = response.data.length;
          } else {
            // 使用默认数据
            products.value = getDefaultProducts();
            total.value = products.value.length;
          }
        } else {
          // 没有数据时返回空数组
          products.value = [];
          total.value = 0;
        }
      } catch (error) {
        console.error("加载产品数据失败:", error);
        // 出错时返回空数组
        products.value = [];
        total.value = 0;
      }
    };

    // 过滤产品列表 - 现在直接使用后端返回的数据，不需要前端过滤
    const filteredProducts = computed(() => {
      return products.value;
    });

    // 搜索处理
    const handleSearch = () => {
      currentPage.value = 1;
      loadProducts();
    };

    // 分页处理
    const handlePageChange = (page) => {
      currentPage.value = page;
      loadProducts();
    };

    // 显示产品详情
    const showProductDetail = (product) => {
      // 跳转到展商详情页面，传递展商ID
      if (product.exhibitorId) {
        router.push(`/exhibitor-detail/${product.exhibitorId}`);
      } else {
        ElMessage.warning("展商信息不完整");
      }
    };

    const loadBanner = async () => {
      try {
        const res = await homepageApi.getProductBannerData();
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          bannerImage.value = res.data;
        }
      } catch (e) {
        bannerImage.value = [];
      }
    };

    onMounted(() => {
      try {
        window.scrollTo({ top: 0, left: 0, behavior: "auto" });
      } catch (_) {
        window.scrollTo(0, 0);
      }
      loadProducts();
      loadBanner();
    });

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "exhibitors-directory") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      activeTab,
      bannerImage,
      defaultProductImage,
      products,
      searchKeyword,
      filteredProducts,
      currentPage,
      pageSize,
      total,
      // totalPages,
      handleSearch,
      handlePageChange,
      showProductDetail,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.exhibitors-directory {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  /* padding-top: 110px; */
  /* padding-bottom: 10px; */
  margin-top: 0;
  text-align: center;
  color: white;
  position: relative;
}

.page-header.has-bg {
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  /* padding-top: 110px; */
  max-width: 100% !important;
  background-color: rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  /* align-items: center; */
  justify-content: center;
}

.banner-img {
  width: 100%;
  height: 100%;
  /* object-fit: cover; */
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.directory-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.directory-content {
  max-width: 1000px;
  margin: 0 auto;
}

.directory-intro {
  margin-bottom: 3rem;
}

/* 搜索框样式 */
.search-section {
  margin-bottom: 30px;
}

.search-container {
  max-width: 600px;
  margin: 0 auto;
}

.search-input {
  width: 100%;
}

.search-input :deep(.el-input__wrapper) {
  border-radius: 25px;
  padding: 8px 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 2px solid #e4e7ed;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__wrapper:hover) {
  border-color: var(--e-global-color-selected);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.search-input :deep(.el-input__wrapper.is-focus) {
  border-color: var(--e-global-color-selected);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.search-input :deep(.el-input__inner) {
  font-size: 16px;
  height: 48px;
  line-height: 48px;
}

.search-stats {
  text-align: center;
  margin-top: 15px;
  color: #666;
  font-size: 14px;
  padding: 8px 16px;
  background: #f0f9ff;
  border-radius: 20px;
  display: inline-block;
}

.directory-intro h3 {
  font-size: 1.8rem;
  color: var(--e-global-color-title);
  margin-bottom: 1rem;
}

.directory-intro p {
  color: #666;
  line-height: 1.8;
  font-size: 1.1rem;
}

.directory-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2rem;
}

.product-item {
  background: white;
  border-radius: 15px;
  /* padding: 1.5rem; */
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 1px solid #e0e7ff;
  cursor: pointer;
  overflow: hidden;
}

.product-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
  border-color: var(--e-global-color-title);
}

.product-image {
  width: 100%;
  padding-bottom: 60%;
  position: relative;
  /* height: 200px; */
  /* border-radius: 10px; */
  overflow: hidden;
  margin-bottom: 1rem;
  /* border: 1px solid #e4e7ed; */
  background: #f8f9fa;
  /* display: flex;
  align-items: center;
  justify-content: center; */
}

.product-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.no-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    135deg,
    var(--e-global-color-5c1ca3d) 0%,
    var(--e-global-color-a3699a4) 100%
  );
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
}

.product-info {
  padding: 10px 12px;
}

.product-info h4 {
  font-size: 1.2rem;
  color: var(--e-global-color-title);
  margin-bottom: 0.5rem;
  font-weight: 600;
  line-height: 1.3;
}

.exhibitor-name {
  color: var(--e-global-color-selected);
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.product-category {
  color: var(--e-global-color-title);
  font-weight: 600;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  padding: 0.3rem 0.8rem;
  border-radius: 15px;
  display: inline-block;
}

.product-details {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
  font-size: 0.8rem;
}

.product-action {
  margin-top: 1rem;
  text-align: right;
}

.view-detail {
  color: var(--e-global-color-title);
  font-weight: 600;
  font-size: 0.9rem;
  transition: color 0.3s ease;
}

.product-item:hover .view-detail {
  color: var(--e-global-color-selected);
}

/* 分页样式 */
.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 3rem;
  padding: 2rem 0;
}

@media (max-width: 768px) {
  .directory-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .directory-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .page-title {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
  }

  .page-subtitle {
    font-size: 1rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

/* 平板端响应式 */
@media (max-width: 1024px) and (min-width: 769px) {
  .directory-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1.8rem;
  }
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
